//
//  KFOrderCell.m
//  KafeeSale
//
//  Created by Liboom on 2025/6/11.
//

#import "KFOrderCell.h"
#import "KFChoseBoardVC.h"
#import "KFRealCartVC.h"
#import "MarqueeLabel.h"
#import "KFSingleAlertView.h"
@interface KFOrderCell()<UITextFieldDelegate>
@property(nonatomic, strong) UIView *bgView,*firstLine;
@property(nonatomic, strong) UILabel *boardNameLab,*orderPriceLab, * hasPayLab,*typeLab,*timeLab,*tmpLab,*indexLab;
@property(nonatomic, strong) UIButton *hasDownBtn, * closeBtn, * switchBtn,*addOrderBtn;
@property(nonatomic, assign) NSInteger curGoodTag;

@end
@implementation KFOrderCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = UnderLineColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCusUI];
    }
    return self;
}
-(void)createCusUI{
    self.bgView = [UIView new];
    self.bgView.backgroundColor = UIColor.whiteColor;
    self.bgView.cornerRadius = 10;
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout.leftSpaceToView(self.contentView, 10).rightSpaceToView(self.contentView, 10).topSpaceToView(self.contentView, 5);
    
//    UIImageView * imgV = [UIImageView new];
//    imgV.image = Image(@"桌台sel");
//    [self.bgView addSubview:imgV];
//    imgV.sd_layout.leftSpaceToView(self.bgView, 10).topSpaceToView(self.bgView, 15).heightIs(20).widthEqualToHeight();
    
//    self.indexLab = [UILabel labelWithTitle:@"" color:[UIColor blackColor] font:[UIFont boldSystemFontOfSize:20]];
//    [self.bgView addSubview:self.indexLab];
//    self.indexLab.sd_layout.leftSpaceToView(self.bgView, 15).topSpaceToView(self.bgView, 15).heightIs(20);
//    [self.indexLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.boardNameLab = [UILabel labelWithTitle:@"" color:RED_COLOR font:[UIFont boldSystemFontOfSize:22]];
    [self.bgView addSubview:self.boardNameLab];
    self.boardNameLab.sd_layout.leftSpaceToView(self.bgView, 15).topSpaceToView(self.bgView, 15).heightIs(20);
    [self.boardNameLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.switchBtn.backgroundColor = [UIColor whiteColor];
    [self.switchBtn setTitle:@"换桌" forState:UIControlStateNormal];
    [self.switchBtn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    self.switchBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.switchBtn addTarget:self action:@selector(swBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.switchBtn setImage:[UIImage imageNamed:@"04转换"] forState:UIControlStateNormal];
    self.switchBtn.cornerRadius = 5;
    self.switchBtn.borderColor = UIColor.lightGrayColor;
    self.switchBtn.borderWidth = 1;
    [self.bgView addSubview:self.switchBtn];
    self.switchBtn.sd_layout.leftSpaceToView(self.boardNameLab, 20).centerYEqualToView(self.boardNameLab).heightIs(30).widthIs(60);
    
    self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeBtn.backgroundColor = [UIColor colorWithRed:0.26 green:0.72 blue:0.29 alpha:1];
    [self.closeBtn setTitle:@"已完成" forState:UIControlStateNormal];
    self.closeBtn.cornerRadius = 10;
    [self.closeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.closeBtn addTarget:self action:@selector(closeOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.closeBtn];
    self.closeBtn.sd_layout.leftSpaceToView(self.switchBtn, 5).centerYEqualToView(self.switchBtn).widthIs(75).heightIs(30);
    
    self.typeLab = [UILabel labelWithTitle:@"" color:UIColor.blackColor fontSize:15];
    [self.bgView addSubview:self.typeLab];
    self.typeLab.sd_layout.rightSpaceToView(self.bgView, 10).centerYEqualToView(self.boardNameLab).heightIs(20);
    [self.typeLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.addOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addOrderBtn.backgroundColor = [UIColor colorWithRed:0.15 green:0.61 blue:1 alpha:1];
    [self.addOrderBtn setTitle:@"加单" forState:UIControlStateNormal];
    [self.addOrderBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.addOrderBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.addOrderBtn.cornerRadius = 10;
    [self.addOrderBtn addTarget:self action:@selector(addOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.addOrderBtn];
    self.addOrderBtn.sd_layout.rightSpaceToView(self.bgView, 10).topSpaceToView(self.typeLab, 5).widthIs(75).heightIs(30);
    
    self.hasDownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.hasDownBtn.backgroundColor = [UIColor colorWithRed:0.05 green:0.76 blue:0.38 alpha:1];
    [self.hasDownBtn setTitle:@"已完成" forState:UIControlStateNormal];
    self.hasDownBtn.cornerRadius = 10;
    self.hasDownBtn.hidden = YES;
    [self.hasDownBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.hasDownBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.hasDownBtn addTarget:self action:@selector(hasDoneClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.hasDownBtn];
    self.hasDownBtn.sd_layout.leftEqualToView(self.addOrderBtn).rightEqualToView(self.addOrderBtn).topSpaceToView(self.addOrderBtn, 10).heightIs(30);
    
    UIImageView * imgV4 = [UIImageView new];
    imgV4.image = Image(@"时间");
    [self.bgView addSubview:imgV4];
    imgV4.sd_layout.leftSpaceToView(self.bgView, 13).topSpaceToView(self.boardNameLab, 10).heightIs(15).widthEqualToHeight();
    
    self.timeLab = [UILabel labelWithTitle:@"" color:UIColor.blackColor fontSize:14];
    [self.bgView addSubview:self.timeLab];
    self.timeLab.sd_layout.leftSpaceToView(imgV4, 5).centerYEqualToView(imgV4).heightIs(20);
    [self.timeLab setSingleLineAutoResizeWithMaxWidth:300];
    
    UIImageView * imgV3 = [UIImageView new];
    imgV3.image = Image(@"金币");
    [self.bgView addSubview:imgV3];
    imgV3.sd_layout.leftSpaceToView(self.bgView, 10).topSpaceToView(imgV4, 10).heightIs(20).widthEqualToHeight();
    
    self.tmpLab = [UILabel labelWithTitle:@"已付金额：" color:UIColor.blackColor fontSize:14];
    [self.bgView addSubview:self.tmpLab];
    self.tmpLab.sd_layout.leftSpaceToView(imgV3, 5).centerYEqualToView(imgV3).heightIs(20);
    [self.tmpLab setSingleLineAutoResizeWithMaxWidth:300];
    
    self.hasPayLab = [UILabel labelWithTitle:@"" color:RED_COLOR font:[UIFont boldSystemFontOfSize:18]];
    [self.bgView addSubview:self.hasPayLab];
    self.hasPayLab.sd_layout.leftSpaceToView(self.tmpLab, 0).bottomEqualToView(self.tmpLab).heightIs(20);
    [self.hasPayLab setSingleLineAutoResizeWithMaxWidth:300];
    
    
    self.firstLine = [UIView new];
    self.firstLine.backgroundColor = UnderLineColor;
    [self.bgView addSubview:self.firstLine];
    
}
-(void)swBtnClick{
    KFChoseBoardVC * vc = [KFChoseBoardVC new];
    vc.boardId = self.model.boardId;
    vc.orderId = self.model.tid;
    [[Mytools currentViewController].navigationController pushViewController:vc animated:YES];
}
- (void)setModel:(KFOrderModel *)model{
    _model = model;
//    self.hasDownBtn.hidden = model.state < 4;
    self.closeBtn.hidden = model.state != 4;
    self.addOrderBtn.hidden = model.state > 4;
    self.switchBtn.hidden = model.state > 4;
//    self.indexLab.text = [NSString stringWithFormat:@"%ld", model.serialNumber];
    NSInteger count =0;
    for (int i =0; i<model.items.count; i++) {
        KFOrderGoodsModel * mo = model.items[i];
        if (mo.dinnerState>1) {
            count ++;
        }
    }
    self.firstLine.sd_layout.leftSpaceToView(self.bgView, 10).rightSpaceToView(self.bgView, 10).topSpaceToView(self.hasDownBtn.isHidden?self.hasDownBtn:@[self.hasPayLab,self.hasDownBtn], 10).heightIs(1);
    
    self.boardNameLab.text = model.boardName;
    NSString * typeStr = model.type == 0?@"堂食":@"外卖";
    
    NSString * stateStr;
    if (model.state == 0) {
        stateStr = @"已创建";        // CREATE
    } else if (model.state == 1) {
        stateStr = @"待支付";        // UNPAID
    } else if (model.state == 2) {
        stateStr = @"已支付";        // PAID (原代码中将 state=2 错误映射为"已完成")
    } else if (model.state == 3) {
        stateStr = @"已取消";        // CANCEL
    } else if (model.state == 4) {
        stateStr = @"已完成";        // FINISHED (新增状态)
    } else if (model.state == 5) {
        stateStr = @"已关闭";        // CLOSED (新增状态)
    } else if (model.state == 99) {
        stateStr = @"已过期";        // EXPIRE
    } else {
        stateStr = @"";
    }
    self.typeLab.text = [NSString stringWithFormat:@"%@/%@",typeStr,stateStr];
    self.orderPriceLab.text = [NSString stringWithFormat:@"订单金额：￥%.2lf",model.amount];
   
    if(model.state == 2){
        self.tmpLab.text = @"已付金额：";
    }else{
        self.tmpLab.text = @"实付金额：";
    }
    self.hasPayLab.text = [NSString stringWithFormat:@"￥%.2lf",model.payAmount];;
    self.timeLab.text = [NSString stringWithFormat:@"下单时间：%@",model.createTime];
    for (UIView*v in self.bgView.subviews) {
        if(v.tag>99){
            [v removeFromSuperview];
        }
    }
    if(model.items.count){
        UIView * lastV = nil;
        for (int i =0;i<model.items.count; i++) {
            KFOrderGoodsModel * mo = model.items[i];
            UIView *v = [UIView new];
            v.tag = 100+i;
            [self.bgView addSubview:v];
            v.sd_layout.leftEqualToView(self.bgView).rightEqualToView(self.bgView).topSpaceToView(lastV?:self.firstLine, lastV?0:10);
            
            UIImageView * closeImg = [UIImageView new];
            closeImg.image = Image(@"关闭_圈");
            closeImg.tag = 101 + i;
            closeImg.hidden = mo.dinnerState >0;
            [closeImg addTarget:self action:@selector(refundBtnClick:)];
            [v addSubview:closeImg];
            closeImg.sd_layout.leftSpaceToView(v, 10).topSpaceToView(v, 10).widthIs(20).heightEqualToWidth();
            
            UIImageView * doneArrow = [UIImageView new];
            doneArrow.image = mo.dinnerState==2? Image(@"选中"):mo.dinnerState==3?Image(@"退回"):nil;
            doneArrow.hidden = !(mo.dinnerState==2||mo.dinnerState==3);
            [v addSubview:doneArrow];
            doneArrow.sd_layout.leftSpaceToView(closeImg.isHidden?v:closeImg, 10).topSpaceToView(v, 10).heightIs(20).widthIs(20);
            
            UILabel * nameLab = [UILabel labelWithTitle:[NSString stringWithFormat:@"%@   X%ld",mo.goodsName,mo.num] color:UIColor.blackColor fontSize:18];
            [v addSubview: nameLab];
            nameLab.sd_layout.leftSpaceToView(doneArrow.isHidden?(closeImg.isHidden?v:closeImg):doneArrow, 10).topSpaceToView(v, 10).heightIs(20);
            [nameLab setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH-200];
            
            // 添加规格和做法信息展示 - 左对齐，字号14
            NSMutableArray *infoLabels = [NSMutableArray array];
            
            // ------------------- 新增代码：开始 -------------------
            // 展示豆子信息
            // 假设 mo.bean 是一个字符串或可以转换成字符串的类型
            // 为了安全起见，我们检查它是否存在并且转换成字符串后长度大于0
            if (mo.bean && [[mo.bean description] length] > 0) {
                VicSingleObject * single = [VicSingleObject getInstance];
                NSString * beanName = @"";
                for (int i =0; i<single.beansList.count; i++) {
                    NSDictionary * dict = single.beansList[i];
                    if ([dict[@"beanValue"] isEqualToString:mo.bean]) {
                        beanName = dict[@"beanName"];
                        break;
                    }
                }
                NSString *beanText = [NSString stringWithFormat:@"豆子：%@", beanName];
                // 使用与其他信息统一的字体大小和颜色
                UILabel *beanLab = [UILabel labelWithTitle:beanText color:UIColor.grayColor fontSize:16];
                beanLab.numberOfLines = 0;
                beanLab.textAlignment = NSTextAlignmentLeft;
                [v addSubview:beanLab];
                [infoLabels addObject:beanLab]; // 第一个添加到数组中，确保显示在最上面
            }
            // ------------------- 新增代码：结束 -------------------
            
            // 展示规格信息
            // 展示规格信息
            if (mo.specName && mo.specName.length > 0) {
                // 计算完整价格：基础价格 + 规格加价 + 做法加价
                CGFloat basePrice = mo.price; // 商品基础价格
                CGFloat specPrice = mo.specPrice ; // 规格加价
                CGFloat practicePrice = 0; // 做法加价总和
                
                // 计算所有做法的加价
                if (mo.cooks && [mo.cooks isKindOfClass:[NSDictionary class]]) {
                    NSArray *cooksItems = mo.cooks[@"cooksItems"];
                    if (cooksItems && [cooksItems isKindOfClass:[NSArray class]]) {
                        for (NSDictionary *cookItem in cooksItems) {
                            NSNumber *cookPrice = cookItem[@"cooksPrice"];
                            if (cookPrice && [cookPrice floatValue] > 0) {
                                practicePrice += [cookPrice floatValue];
                            }
                        }
                    }
                }
                
                // 计算单份商品总价
                CGFloat singleTotalPrice = basePrice + specPrice + practicePrice;
                
                NSString *specText = [NSString stringWithFormat:@"规格：%@ ￥%.0lf", mo.specName, singleTotalPrice];
                
                UILabel *specLab = [UILabel labelWithTitle:specText color:UIColor.grayColor fontSize:16];
                specLab.numberOfLines = 0;
                specLab.textAlignment = NSTextAlignmentLeft;
                [v addSubview:specLab];
                [infoLabels addObject:specLab];
            }
            
            // 展示做法信息
            if (mo.cooks && [mo.cooks isKindOfClass:[NSDictionary class]]) {
                NSString *cooksTitle = @"做法";
                NSArray *cooksItems = mo.cooks[@"cooksItems"];
                
                if (cooksTitle && cooksItems && [cooksItems isKindOfClass:[NSArray class]] && cooksItems.count > 0) {
                    NSMutableString *practiceText = [NSMutableString stringWithFormat:@"%@：", cooksTitle];
                    
                    for (NSDictionary *cookItem in cooksItems) {
                        NSString *cookName = cookItem[@"cooksName"];
                        NSNumber *cookPrice = cookItem[@"cooksPrice"];
                        
                        if (cookName) {
                            if ([practiceText length] > [cooksTitle length] + 2) {
                                [practiceText appendString:@"、"];
                            }
                            [practiceText appendString:cookName];
                            
                            // 只有当价格大于0时才显示价格
                            if (cookPrice && [cookPrice floatValue] > 0) {
                                [practiceText appendFormat:@"(￥%.0lf)", [cookPrice floatValue]];
                            }
                        }
                    }
                    
                    UILabel *practiceLab = [UILabel labelWithTitle:practiceText color:UIColor.grayColor fontSize:16];
                    practiceLab.numberOfLines = 0;
                    practiceLab.textAlignment = NSTextAlignmentLeft;
                    [v addSubview:practiceLab];
                    [infoLabels addObject:practiceLab];
                }
            }
            
            // 布局规格和做法标签 - 左对齐
            UIView *lastInfoView = nameLab;
            for (UILabel *infoLab in infoLabels) {
                infoLab.sd_layout
                .leftEqualToView(nameLab)
                .topSpaceToView(lastInfoView, 5)
                .rightSpaceToView(v, 10)
                .autoHeightRatio(0);
                lastInfoView = infoLab;
            }
            
            // 计算总价：基础价格 + 规格价格 + 做法价格
            CGFloat totalPrice =[mo.amount floatValue];
            
            // 加上规格价格
            if (mo.specPrice  > 0) {
                totalPrice += mo.specPrice;
            }
            
            // 加上做法价格
            if (mo.cooks && [mo.cooks isKindOfClass:[NSDictionary class]]) {
                NSArray *cooksItems = mo.cooks[@"cooksItems"];
                if (cooksItems && [cooksItems isKindOfClass:[NSArray class]]) {
                    for (NSDictionary *cookItem in cooksItems) {
                        NSNumber *cookPrice = cookItem[@"cooksPrice"];
                        if (cookPrice && [cookPrice floatValue] > 0) {
                            totalPrice += [cookPrice floatValue];
                        }
                    }
                }
            }
            
            // 乘以数量
            totalPrice = totalPrice * mo.num;
            
            UILabel * priceLab = [UILabel labelWithTitle:[NSString stringWithFormat:@"￥%.2lf", totalPrice] color:UIColor.blackColor fontSize:16];
            [v addSubview: priceLab];
            priceLab.sd_layout.rightSpaceToView(v, 10).centerYEqualToView(nameLab).heightIs(20);
            [priceLab setSingleLineAutoResizeWithMaxWidth:300];
            
            // 调整备注标签的位置，确保在规格做法信息下方
            UILabel * remarkLab = [UILabel labelWithTitle:@"备注：" color:UIColor.grayColor fontSize:14];
            [v addSubview:remarkLab];
            remarkLab.tag = 204+i;
            remarkLab.textAlignment = NSTextAlignmentLeft;
            
            // 根据是否有规格做法信息调整备注位置
            if (infoLabels.count > 0) {
                remarkLab.sd_layout.leftSpaceToView(v, 10).topSpaceToView(lastInfoView, 10).heightIs(20);
            } else {
                remarkLab.sd_layout.leftSpaceToView(v, 10).topSpaceToView(nameLab, 10).heightIs(20);
            }
            [remarkLab setSingleLineAutoResizeWithMaxWidth:300];
            
            UITextField * tf = [UITextField new];
            tf.delegate = self;
            tf.textColor = UIColor.blackColor;
            tf.font = [UIFont systemFontOfSize:14];
            tf.tag = 207+i;
            tf.textAlignment = NSTextAlignmentLeft;
            [v addSubview:tf];
            if (mo.remark) {
                tf.text = mo.remark;
            }
            tf.sd_layout.leftSpaceToView(remarkLab, 0).centerYEqualToView(remarkLab).heightIs(40).widthIs(0.5*SCREEN_WIDTH);
            
            // 调整出餐按钮位置
            UIButton * outBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            outBtn.backgroundColor = mo.dinnerState==0?[UIColor colorWithRed:0.49 green:0.7 blue:0.27 alpha:1]:[UIColor colorWithRed:0.9 green:0.4 blue:0.13 alpha:1];
            [outBtn setTitle:mo.dinnerState==0?@"出餐":mo.dinnerState==1?@"准备中":@"" forState:UIControlStateNormal];
            outBtn.cornerRadius = 10;
            outBtn.hidden = mo.dinnerState > 1;
            [outBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            outBtn.tag = 201+i;
            outBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            [outBtn addTarget:self action:@selector(outClick:) forControlEvents:UIControlEventTouchUpInside];
            [v addSubview:outBtn];
            
            // 根据是否有规格做法信息调整按钮位置
            outBtn.sd_layout.rightSpaceToView(v, 10).centerYEqualToView(tf).widthIs(75).heightIs(30);
            
            if (i< model.items.count -1) {
                UIView*line = [UIView new];
                line.backgroundColor = UnderLineColor;
                [v addSubview:line];
                line.sd_layout.leftSpaceToView(v, 10).rightSpaceToView(v, 10).bottomEqualToView(v).heightIs(1);
            }
            
            // 设置视图高度
            [v setupAutoHeightWithBottomView:outBtn bottomMargin:10];
            lastV = v;
        }
        [self.bgView setupAutoHeightWithBottomView:lastV bottomMargin:10];
    }else{
        [self.bgView setupAutoHeightWithBottomViewsArray:@[self.timeLab,self.closeBtn] bottomMargin:10];
    }
    [self setupAutoHeightWithBottomView:self.bgView bottomMargin:0];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length  == 0) {
        return;
    }
    KFOrderGoodsModel * mo = self.model.items[textField.tag - 207];
    NSString * url = [WorkUrl returnURL:Interface_For_Update_Order_Remark];
    NSDictionary * dic = @{@"id":@(mo.tid),@"remark":textField.text};
    XHQHUDInView([Mytools currentViewController].view);
    [PGNetworkHelper POST:url parameters:dic cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
        XHQHUDHide();
        if ([responseObject[@"code"] integerValue]==200) {
            XHQHUDSuccessText(@"备注添加成功");
            self.reloadBlock();
        }else{
            XHQHUDErrorText(responseObject[@"msg"]);
        }
    } failure:^(id error) {
        XHQHUDHide();
    }];
}
-(void)refundBtnClick:(UITapGestureRecognizer*)tap{
    KFSingleAlertView * v = [[KFSingleAlertView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [[Mytools currentViewController].view addSubview:v];
    v.msgString = @"是否确认关闭订单";
    __weak typeof(v)weakSelf = v;
    v.actionBlock = ^(NSInteger type) {
        if(type == 1){
            KFOrderGoodsModel * mo = self.model.items[tap.view.tag - 101];
            [PGNetworkHelper POST:[WorkUrl returnURL:Interface_For_Post_Refund_Dish] parameters:@{@"id":@(mo.tid)} cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
                if ([responseObject[@"code"] integerValue]==200) {
                    XHQHUDSuccessText(@"退品成功");
                    self.reloadBlock();
                }else{
                    XHQHUDErrorText(responseObject[@"msg"]);
                }
            } failure:^(id error) {
                
            }];
        }
        [weakSelf removeFromSuperview];
    };
    
}
//已完成
-(void)hasDoneClick{
    
}
-(void)addOrder{
    KFRealCartVC  * vc = [KFRealCartVC new];
    vc.orderId = self.model.tid;
    vc.boardId = [NSString stringWithFormat:@"%ld",self.model.boardId];
    vc.dinnerCount = @"1";
    [[Mytools currentViewController].navigationController pushViewController:vc animated:YES];
}
-(void)outClick:(UIButton*)sender{
    KFOrderGoodsModel * mo = self.model.items[sender.tag - 201];
    XHQHUDInView([Mytools currentViewController].view);
    NSInteger dState = mo.dinnerState==0?1:mo.dinnerState==1?2:3;
    [PGNetworkHelper POST:[WorkUrl returnURL:Interface_For_Order_Out] parameters:@{@"id":@(mo.tid),@"dinnerState":@(dState)} cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
        XHQHUDHide();
        if ([responseObject[@"code"] integerValue]==200) {
            XHQHUDSuccessText(@"出餐成功");
            self.reloadBlock();
        }else{
            XHQHUDErrorText(responseObject[@"msg"]);
        }
    } failure:^(id error) {
        XHQHUDHide();
    }];
}
-(void)closeOrder{
    KFSingleAlertView * v = [[KFSingleAlertView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [[Mytools currentViewController].view addSubview:v];
    v.msgString = @"是否确认关闭订单";
    __weak typeof(v)weakSelf = v;
    v.actionBlock = ^(NSInteger type) {
        if(type == 1){
            [self finalClose];
        }
        [weakSelf removeFromSuperview];
    };
//    BOOL hasPas = NO;
//    for (int i =0;i<self.model.items.count; i++) {
//        KFOrderGoodsModel * mo = self.model.items[i];
//        if (mo.dinnerState == 1) {
//            hasPas = YES;
//            break;
//        }
//    }
//    if (hasPas) {
//        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"当前已出餐，是否确定取消订单？" preferredStyle:UIAlertControllerStyleAlert];
//        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//            [self finalClose];
//        }]];
//        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"cancel");
//        }]];
//        [[Mytools currentViewController] presentViewController:alert animated:YES completion:nil];
//    }else{
//        [self finalClose];
//    }
}
-(void)finalClose{
    XHQHUDInView([Mytools currentViewController].view);
    [PGNetworkHelper POST:[WorkUrl returnURL:Interface_For_Order_Close] parameters:@{@"orderId":@(self.model.tid)} cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
        XHQHUDHide();
        if ([responseObject[@"code"] integerValue]==200) {
            XHQHUDSuccessText(@"出餐成功");
            self.reloadBlock();
        }else{
            XHQHUDErrorText(responseObject[@"msg"]);
        }
    } failure:^(id error) {
        XHQHUDHide();
    }];
}

@end
